package com.itheima.controller;

import com.itheima.domain.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@Controller
public class AjaxController {

    @RequestMapping("/ajaxController")
    public String ajaxController(){
        System.out.println("ajax request is running...");
        return "page.jsp";
    }



 /*   @RequestMapping("/ajaxContoller")
    public String ajaxController(@RequestBody String message){
        System.out.println("ajax request is running..."+message);
        return "page.jsp";
    }
*/
 public String ajaxController(@RequestBody String message){
     System.out.println("ajax request is running..."+message);
     return "page.jsp";
 }


  /*  @RequestMapping("/ajaxPojoToController")

    public String ajaxPojoToController(@RequestBody User user){
        System.out.println("controller pojo :"+user);
        return "page.jsp";
    }*/

  @RequestMapping("/ajaxPojoToController")
  public String ajaxPojoToController(@RequestBody User user){
      System.out.println("controller pojo :"+user);
      return "page.jsp";
  }


  /*  @RequestMapping("/ajaxListToController")

    public String ajaxListToController(@RequestBody List<User> userList){
        System.out.println("controller list :"+userList);
        return "page.jsp";
    }*/
  @RequestMapping("/ajaxListToController")
  public String ajaxListToController(@RequestBody List<User> userList){
      System.out.println("controller list :"+userList);
      return "page.jsp";
  }



  /*  @RequestMapping("/ajaxReturnString")
    public @ResponseBody
    String ajaxReturnString(){
        System.out.println("controller return string ...");
        return "page.jsp";
    }*/

  @RequestMapping("/ajaxReturnString")
    public @ResponseBody String ajaxReturnString(){
      System.out.println("controller return string ...");
      return "page.jsp";
  }

/*    @RequestMapping("/ajaxReturnJson")
    @ResponseBody
    public User ajaxReturnJson(){
        System.out.println("controller return json pojo...");
        User user = new User();
        user.setName("Jockme");
        user.setAge(39);
        return user;
    }*/

public User ajaxReturnJson(){
    System.out.println("controller return json pojo...");
    User user = new User();
    user.setName("Jockme");
    user.setAge(39);
    return user;
}


    @RequestMapping("/ajaxReturnJsonList")
    @ResponseBody
    public List ajaxReturnJsonList(){
        System.out.println("controller return json list...");
        User user1 = new User();
        user1.setName("Tom");
        user1.setAge(3);

        User user2 = new User();
        user2.setName("Jerry");
        user2.setAge(5);

        ArrayList al = new ArrayList();
        al.add(user1);
        al.add(user2);

        return al;
    }


    @RequestMapping("/cross")
    @ResponseBody
    @CrossOrigin
    public User cross(HttpServletRequest request){
        System.out.println("controller cross..."+request.getRequestURL());
        User user = new User();
        user.setName("Jockme");
        user.setAge(39);
        return user;
    }

}
